#!/usr/bin/env python

import datetime
import cPickle
from dal.fitsdatasource import *

def loadAstrox(fileName):
	"""Load an instance of Astrox from a file"""
	return cPickle.load(open(fileName, 'rb'))	

class Astrox(object):
	"""The main Astronomex object, describing a astronomical DataSet.
	An Astrox instance can be serialized into a .astrox file.
	An Astrox instance contains:
	 - the physical description of an observation stored in the calibration graph.
	 - the links to use to access the DataSource and metadata
	 - other information such as last modification date etc.
	"""
	def __init__(self):
		# The dict of id : DataSource
		self.dataSources = {}
		# The last modif date/time
		self.lastModifDate = datetime.datetime.utcnow()
		# The calibration graph for our observation
		self.calibGraph = None
		
	def save(self,fileName):
		"""Save to a file"""
		cPickle.dump(self,open(fileName,'w'))
		
	def getMetaData(self):
		"""Return a key:value dictionary containing all the metadata associated with the DataSet.
		For astronomex, the metadata are key:values pairs or free text without any meaning, i.e
		astronomex does not use and understand the meaning of its content.
		"""
		result = {}
		for d in self.dataSources:
			result += d.getMetaData()
		return result


if __name__ == '__main__':
	a = Astrox()
	a.dataSources['image'] = FITSDataSource('data/image.fits')
	a.dataSources['spectrum'] = FITSDataSource('data/spectrum.fits')
	a.save('data/test.astrox')
	b = loadAstrox('data/test.astrox')
	print b.dataSources
	print b.dataSources['image'].getDataShape()

